﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Kaidou.Model;
using Kaidou.Persister;
using Kaidou.Util;
using Kaidou.Bll;
using System.Collections.Generic;
using System.Globalization;

public partial class seal_seal_order_detail : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        LoginModel loginModel = (LoginModel)Session["loginModel"];
        if (loginModel == null)
        {
            Response.Redirect("../login.aspx");
        }
        else if (loginModel.isKumiaiin())
        {
            Response.Redirect("../menu.aspx");
        }

        if (loginModel.isKumiaiin() && sessioncontrol.control())
        {
            Response.Redirect("../control.aspx");
        }

        if (!IsPostBack)
        {
            try
            {

                Order order = new Order();
                TRN_Juchu_HPersister parH = new TRN_Juchu_HPersister();
                TRN_Juchu_DPersister parD = new TRN_Juchu_DPersister();
                string juchu_no = Request.QueryString["juchu_no"].ToString();
                TrnJuchuh h = parH.getDetail(juchu_no);
                TrnJuchud[] d = parD.getDetail(juchu_no);
                order.setJuchu_H(h);
                order.setJuchu_D(d);

                if (!TrnJuchuh.JUCHU_STATUS_NOTORDERD.Equals(h.JuchuSts))
                {

                    //発注済詳細
                    Response.Redirect("seal_ordered_detail.aspx?juchu_no=" + juchu_no);

                    return;
                }

                MST_UnionPersister dao1 = new MST_UnionPersister();
                MST_Union_BranchPersister dao2 = new MST_Union_BranchPersister();
                MstUnion union = dao1.getDetail(h.UnionCode);
                mst_union_branch branch = dao2.getDetail(h.UnionCode, h.BranchCode);

                Session["order"] = order;

               // count.Text = Session["ORDER_SESSION_COUNTER"] == null ? "0" : Session["ORDER_SESSION_COUNTER"].ToString();
                count.Text = h.UpdNum.ToString();
                if (TrnJuchuh.JUCHU_CLSS_INTERNET.Equals(h.JuchuClss))
                {
                    htitle.Text = "インターネット";
                }
                else if (TrnJuchuh.JUCHU_CLSS_FAX.Equals(h.JuchuClss))
                {
                    htitle.Text = "FAX/OCR";
                }

                JUCHU_YY.Text = ((DateTime)h.JuchuDate).ToString("yy");
                JUCHU_MM.Text = ((DateTime)h.JuchuDate).ToString("MM");
                JUCHU_DD.Text = ((DateTime)h.JuchuDate).ToString("dd");

                MST_UNION_CODE.Text = h.UnionCode;
                ParentOfficeName.Text = union == null ? "" : union.OfficeName;
                MST_BRANCH_CODE.Text = h.BranchCode == 0 ? "000" : h.BranchCode.ToString();
                Branch_office_name.Text = branch == null ? "本店" : branch.officename;
                TRN_JUCHU_TANTO_NAME.Text = h.JuchuTantoName;

                if (h.HopeDate != null)
                {
                    DateTime dt = (DateTime)h.HopeDate;
                    TRN_HOPE_YEAR.Text = dt.ToString("yy");
                    TRN_HOPE_MONTH.Text = dt.ToString("MM");
                    TRN_HOPE_DAY.Text = dt.ToString("dd");
                }

                if (TrnJuchuh.HOPE_TIME_CLSS_NONE.Equals(h.HopeTimeClss))
                {
                    TRN_HOPE_TIME_CLSS1.Checked = true;
                }

                if (TrnJuchuh.HOPE_TIME_CLSS_MORNING.Equals(h.HopeTimeClss))
                {
                    TRN_HOPE_TIME_CLSS2.Checked = true;
                }

                if (TrnJuchuh.HOPE_TIME_CLSS_AFTERNOON.Equals(h.HopeTimeClss))
                {
                    TRN_HOPE_TIME_CLSS3.Checked = true;
                }

                string area = Resources.Resource.area;
                string[] areaArray = area.Split(',');

                if (branch != null)
                {
                    addressmsg.Text = "〒" + branch.officezipno.Substring(0, 3) + "-" + branch.officezipno.Substring(3, 4) + "<br/>" + areaArray[branch.officeprefcode-1] + " " + branch.officecity + " " + branch.officeaddress + "<br/>" +
                        "TEL：" + branch.officetel + " FAX：" + branch.officefax;
                }else if(h.BranchCode==0 && union!=null){
                    addressmsg.Text = "〒" + union.OfficeZipNo.Substring(0, 3) + "-" + union.OfficeZipNo.Substring(3, 4) + "<br/>" + areaArray[union.OfficePrefCode-1] + " " + union.OfficeCity + " " + union.OfficeAddress + "<br/>" +
                        "TEL：" + union.OfficeTel + " FAX：" + union.OfficeFax;
                }
                else
                {
                    addressmsg.Text = "組合員が見つかりません。";
                }

                TRN_SEAL_QTY1.Text = d[0] == null ? "" : d[0].SealQty.ToString();

                Kaidou.Persister.MST_TraderPersister traderDao = new Kaidou.Persister.MST_TraderPersister();
                MstTrader trader = null;

                if (d[1] != null)
                {
                    trader = traderDao.getDetail(d[1].TraderRegNo);
                    TRADER_REG_NO12.Text = trader != null ? trader.TraderRegNo.Substring(0, 2) : "";
                    TRADER_REG_NO22.Text = trader != null ? trader.TraderRegNo.Substring(2, 5) : "";
                    TRADER_REG_NO_LABEL2.Text = trader != null ? trader.TraderName : "";
                    TRN_SEAL_QTY2.Text = d[1] != null ? d[1].SealQty.ToString() : "";
                }

                if (d[2] != null)
                {
                    trader = traderDao.getDetail(d[2].TraderRegNo);
                    TRADER_REG_NO13.Text = trader != null ? trader.TraderRegNo.Substring(0, 2) : "";
                    TRADER_REG_NO23.Text = trader != null ? trader.TraderRegNo.Substring(2, 5) : "";
                    TRADER_REG_NO_LABEL3.Text = trader != null ? trader.TraderName : "";
                    TRN_SEAL_QTY3.Text = d[2] != null ? d[2].SealQty.ToString() : "";
                }

                if (d[3] != null)
                {
                    trader = traderDao.getDetail(d[3].TraderRegNo);
                    TRADER_REG_NO14.Text = trader != null ? trader.TraderRegNo.Substring(0, 2) : "";
                    TRADER_REG_NO24.Text = trader != null ? trader.TraderRegNo.Substring(2, 5) : "";
                    TRADER_REG_NO_LABEL4.Text = trader != null ? trader.TraderName : "";
                    TRN_SEAL_QTY4.Text = d[3] != null ? d[3].SealQty.ToString() : "";
                }

                if (d[4] != null)
                {
                    trader = traderDao.getDetail(d[4].TraderRegNo);
                    TRADER_REG_NO15.Text = trader != null ? trader.TraderRegNo.Substring(0, 2) : "";
                    TRADER_REG_NO25.Text = trader != null ? trader.TraderRegNo.Substring(2, 5) : "";
                    TRADER_REG_NO_LABEL5.Text = trader != null ? trader.TraderName : "";
                    TRN_SEAL_QTY5.Text = d[4] != null ? d[4].SealQty.ToString() : "";
                }

                sum.Text = h.TotalSealQty.ToString();

                //OCR_ORDER_QTY.Text = h.OcrOrderQty == 0 ? "" : h.OcrOrderQty.ToString();

            }
            catch (Exception ex)
            {

                LogManager.error("seal_seal_order_detail " + ex.Message + ex.StackTrace);
            }
        }
    }

    protected void check( string mod )
    {
        Order order = (Order)Session["order"];

        try {
			
			TrnJuchuh h = order.getJuchu_H();
			
			//モデルに値をセットする。(TRN_Juchu_H)
			h.UnionCode=MST_UNION_CODE.Text.Trim().Length==0?null:MST_UNION_CODE.Text;
            h.UpdNum =(short) (h.UpdNum + 1);
			//h.JuchuDate=null;
			h.JuchuYear=JUCHU_YY.Text;
            h.JuchuMonth=JUCHU_MM.Text;
            h.JuchuDay=JUCHU_DD.Text;

            if (StringUtil.isValidDate(JUCHU_YY.Text, JUCHU_MM.Text, JUCHU_DD.Text))
            {

                CultureInfo culture = new CultureInfo("ja-JP");
                h.JuchuDate = Convert.ToDateTime((int.Parse(JUCHU_YY.Text) + 2000) + "/" + int.Parse(JUCHU_MM.Text) + "/" + int.Parse(JUCHU_DD.Text, culture));

            }

			if (!ParameterCheckUtility.isNumber(MST_BRANCH_CODE.Text.Trim()))
            {
                h.BranchCode = -1;
            }
            else
            {
               h.BranchCode = MST_BRANCH_CODE.Text.Trim().Length == 0 ? (short)(-1) : short.Parse(MST_BRANCH_CODE.Text);
            }
            h.JuchuTantoName=TRN_JUCHU_TANTO_NAME.Text;
			
            
            h.HopeYear=TRN_HOPE_YEAR.Text;
            h.HopeMonth=TRN_HOPE_MONTH.Text;
            h.HopeDay=TRN_HOPE_DAY.Text;

            if (StringUtil.isValidDate(TRN_HOPE_YEAR.Text, TRN_HOPE_MONTH.Text, TRN_HOPE_DAY.Text))
            {

                CultureInfo culture = new CultureInfo("ja-JP");
                h.HopeDate = Convert.ToDateTime((int.Parse(TRN_HOPE_YEAR.Text) + 2000) + "/" + int.Parse(TRN_HOPE_MONTH.Text) + "/" + int.Parse(TRN_HOPE_DAY.Text, culture));

            }

            h.HopeTimeClss = TRN_HOPE_TIME_CLSS1.Checked ? TrnJuchuh.HOPE_TIME_CLSS_NONE : ( TRN_HOPE_TIME_CLSS2.Checked ? TrnJuchuh.HOPE_TIME_CLSS_MORNING : TrnJuchuh.HOPE_TIME_CLSS_AFTERNOON );

			/*
			string ssq = OCR_ORDER_QTY.Text;
			if (ssq.IndexOf("-") != -1) {
				//マイナスが入力された場合には
				h.OcrOrderQty =-2;
			} else {
                h.OcrOrderQty = OCR_ORDER_QTY.Text.Trim().Length==0?0:int.Parse(OCR_ORDER_QTY.Text);
			}
			if (h.OcrOrderQty == -1)
				h.OcrOrderQty=0;
            */
			TrnJuchud[] model = new TrnJuchud[5];
		    TrnJuchud tmpmodel = null;
            TrnJuchudId id = null;
            string sq = null;

		    //モデルに値をセットする。
			tmpmodel = new TrnJuchud();
            id = new TrnJuchudId();
            id.JuchuNo=h.JuchuNo;
            id.JuchuRecNo=1;
            tmpmodel.Id=id;
            tmpmodel.PrintClss = TrnJuchud.PRINT_CLSS_NO;
            string TraderRegNo = order.getJuchu_D()[0]==null?null: order.getJuchu_D()[0].TraderRegNo;
            tmpmodel.TraderRegNo1 = TraderRegNo == null || TraderRegNo.Length == 0 ? "" : TraderRegNo.Substring(0,2);
            tmpmodel.TraderRegNo2 = TraderRegNo == null || TraderRegNo.Length == 0 ? "" : TraderRegNo.Substring(2, 5);
            tmpmodel.TraderRegNo = TraderRegNo == null || TraderRegNo.Length == 0 ? "" : TraderRegNo;
            sq=TRN_SEAL_QTY1.Text;
            if (StringUtil.isEmpty(sq)) {
				    tmpmodel.SealQty = -1;
			} else if (sq.IndexOf("-") != -1) {
				    //マイナスが入力された場合には
				    tmpmodel.SealQty = -2;
			} else if (!StringUtil.isHankakuNumber(sq)) {
				    tmpmodel.SealQty = -2;
			} else {
                    tmpmodel.SealQty = int.Parse( TRN_SEAL_QTY1.Text );
			}
            if (tmpmodel.SealQty == -1) {
				    model[0] = null;
				    LogManager.info("TRN_JuchuManager getJuchuDFromParameter null! : 1" );
			    } else {
                    LogManager.info("TRN_JuchuManager getJuchuDFromParameter set!  : 1" );
				    model[0] = tmpmodel;
		    }

            //モデルに値をセットする。
            tmpmodel = new TrnJuchud();
            id = new TrnJuchudId();
            id.JuchuNo = h.JuchuNo;
            id.JuchuRecNo = 2;
            tmpmodel.Id = id;
            tmpmodel.PrintClss = TrnJuchud.PRINT_CLSS_YES;
            tmpmodel.TraderRegNo1 = TRADER_REG_NO12.Text;
            tmpmodel.TraderRegNo2 = TRADER_REG_NO22.Text;
            tmpmodel.TraderRegNo = TRADER_REG_NO12.Text + TRADER_REG_NO22.Text;
            sq = TRN_SEAL_QTY2.Text;
            if (StringUtil.isEmpty(sq))
            {
                tmpmodel.SealQty = -1;
            }
            else if (sq.IndexOf("-") != -1)
            {
                //マイナスが入力された場合には
                tmpmodel.SealQty = -2;
            }
            else if (!StringUtil.isHankakuNumber(sq))
            {
                tmpmodel.SealQty = -2;
            }
            else
            {
                tmpmodel.SealQty = int.Parse(TRN_SEAL_QTY2.Text);
            }
            if (tmpmodel.SealQty == -1)
            {
                model[1] = null;
                LogManager.info("TRN_JuchuManager getJuchuDFromParameter null! : 2");
            }
            else
            {
                LogManager.info("TRN_JuchuManager getJuchuDFromParameter set!  : 2");
                model[1] = tmpmodel;
            }

            //モデルに値をセットする。
            tmpmodel = new TrnJuchud();
            id = new TrnJuchudId();
            id.JuchuNo = h.JuchuNo;
            id.JuchuRecNo = 3;
            tmpmodel.Id = id;
            tmpmodel.PrintClss = TrnJuchud.PRINT_CLSS_YES;
            tmpmodel.TraderRegNo1 = TRADER_REG_NO13.Text;
            tmpmodel.TraderRegNo2 = TRADER_REG_NO23.Text;
            tmpmodel.TraderRegNo = TRADER_REG_NO13.Text + TRADER_REG_NO23.Text;
            sq = TRN_SEAL_QTY3.Text;
            if (StringUtil.isEmpty(sq))
            {
                tmpmodel.SealQty = -1;
            }
            else if (sq.IndexOf("-") != -1)
            {
                //マイナスが入力された場合には
                tmpmodel.SealQty = -2;
            }
            else if (!StringUtil.isHankakuNumber(sq))
            {
                tmpmodel.SealQty = -2;
            }
            else
            {
                tmpmodel.SealQty = int.Parse(TRN_SEAL_QTY3.Text);
            }
            if (tmpmodel.SealQty == -1)
            {
                model[2] = null;
                LogManager.info("TRN_JuchuManager getJuchuDFromParameter null! : 3");
            }
            else
            {
                LogManager.info("TRN_JuchuManager getJuchuDFromParameter set!  : 3");
                model[2] = tmpmodel;
            }


            //モデルに値をセットする。
            tmpmodel = new TrnJuchud();
            id = new TrnJuchudId();
            id.JuchuNo = h.JuchuNo;
            id.JuchuRecNo = 4;
            tmpmodel.Id = id;
            tmpmodel.PrintClss = TrnJuchud.PRINT_CLSS_YES;
            tmpmodel.TraderRegNo1 = TRADER_REG_NO14.Text;
            tmpmodel.TraderRegNo2 = TRADER_REG_NO24.Text;
            tmpmodel.TraderRegNo = TRADER_REG_NO14.Text + TRADER_REG_NO24.Text;
            sq = TRN_SEAL_QTY4.Text;
            if (StringUtil.isEmpty(sq))
            {
                tmpmodel.SealQty = -1;
            }
            else if (sq.IndexOf("-") != -1)
            {
                //マイナスが入力された場合には
                tmpmodel.SealQty = -2;
            }
            else if (!StringUtil.isHankakuNumber(sq))
            {
                tmpmodel.SealQty = -2;
            }
            else
            {
                tmpmodel.SealQty = int.Parse(TRN_SEAL_QTY4.Text);
            }
            if (tmpmodel.SealQty == -1)
            {
                model[3] = null;
                LogManager.info("TRN_JuchuManager getJuchuDFromParameter null! : 4");
            }
            else
            {
                LogManager.info("TRN_JuchuManager getJuchuDFromParameter set!  : 4");
                model[3] = tmpmodel;
            }

            //モデルに値をセットする。
            tmpmodel = new TrnJuchud();
            id = new TrnJuchudId();
            id.JuchuNo = h.JuchuNo;
            id.JuchuRecNo = 5;
            tmpmodel.Id = id;
            tmpmodel.PrintClss = TrnJuchud.PRINT_CLSS_YES;
            tmpmodel.TraderRegNo1 = TRADER_REG_NO15.Text;
            tmpmodel.TraderRegNo2 = TRADER_REG_NO25.Text;
            tmpmodel.TraderRegNo = TRADER_REG_NO15.Text + TRADER_REG_NO25.Text;
            sq = TRN_SEAL_QTY5.Text;
            if (StringUtil.isEmpty(sq))
            {
                tmpmodel.SealQty = -1;
            }
            else if (sq.IndexOf("-") != -1)
            {
                //マイナスが入力された場合には
                tmpmodel.SealQty = -2;
            }
            else if (!StringUtil.isHankakuNumber(sq))
            {
                tmpmodel.SealQty = -2;
            }
            else
            {
                tmpmodel.SealQty = int.Parse(TRN_SEAL_QTY5.Text);
            }
            if (tmpmodel.SealQty == -1)
            {
                model[4] = null;
                LogManager.info("TRN_JuchuManager getJuchuDFromParameter null! : 5");
            }
            else
            {
                LogManager.info("TRN_JuchuManager getJuchuDFromParameter set!  : 5");
                model[4] = tmpmodel;
            };

			order.setJuchu_D(model);

			order.setJuchu_H(h);

            int counter = 0;

            if (Session["ORDER_SESSION_COUNTER"] != null)
            {
                counter =(int) Session["ORDER_SESSION_COUNTER"];
            }

            //検証
            LoginModel loginModel = (LoginModel)Session["loginModel"];
            TRN_JuchuManager validation = new TRN_JuchuManager();
            validation.checkUpdateOrder(order, Resources.Resource.ResourceManager, loginModel);
            List<string> errorCodeList = validation.checkUpdateOrder(order, Resources.Resource.ResourceManager, loginModel);
            msg.Text = ErrorCodeUtil.errorCodeListToHtml(errorCodeList) ;

            if (errorCodeList.Count == 0 && TrnJuchuh.JUCHU_STATUS_NOTORDERD.Equals(order.getJuchu_H().JuchuSts))
            {
                //アップデート
                if (mod.Equals("update"))
                {
                    validation.updateOrderForKaidouUser(order, loginModel);
                    counter++;
                    Session["ORDER_SESSION_COUNTER"] = counter;
                    Response.Redirect("seal_order_detail.aspx?juchu_no=" + order.getJuchu_H().JuchuNo);
                }
                //注文の受付処理
                else
                {
                    bool error = false;
                    try
                    {
                        TRN_ReceiptOrderManager1 receiptOrderManager = TRN_ReceiptOrderManager1.checkout();
                        receiptOrderManager.receiptOrder(order , loginModel);
                       // TRN_ReceiptOrderManager1.checkin(receiptOrderManager);
                        TRN_ReceiptOrderManager1.checkin();
                        receiptOrderManager = null;
                    }catch(Exception eee){
                        LogManager.error("seal_order_detail "+eee.Message);
                        error = true;

                    }

                    if (!error)
                    {
                        //発注済詳細
                       // Response.Redirect("seal_ordered_detail.aspx?juchu_no=" + order.getJuchu_H().JuchuNo + "&editMode=rcp");
                       // string url = "seal_ordered_detail.aspx?juchu_no=" + order.getJuchu_H().JuchuNo + "&editMode=rcp";
                        string url = "seal_order_paper.aspx?juchu_no=" + order.getJuchu_H().JuchuNo + "&editMode=rcp";
                        Response.Write("<script language=\"javascript\">window.open('" + url + "','_blank');</script>");
                    }
                    
                }

                
            }
			

		} catch (Exception pe) {
            LogManager.error(" seal_seal_order_detail update "+pe.Message+pe.StackTrace);
			
		} 

    }

    protected void update(object sender, EventArgs e)
    {
        check("update");
    }

    protected void deleteConfirm(object sender, EventArgs e)
    {
        Response.Redirect("seal_order_delete.aspx");
    }

    protected void goBack(object sender, EventArgs e)
    {
        Session["back"] = 1;
        Response.Redirect("seal_order_search.aspx");
    }

    protected void receipt(object sender, EventArgs e)
    {
        
        check("receipt");
    }

}
